package com.warf.warftool.utils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import com.ibm.websphere.crypto.InvalidPasswordDecodingException;
import com.ibm.websphere.crypto.PasswordUtil;
import com.ibm.websphere.crypto.UnsupportedCryptoAlgorithmException;
import com.ibm.ws.security.util.WSEncoderDecoder;
import com.warf.warftool.WarfDeployMain;

public class WarfUtils {
	public static String WARF_PROPERTIES_FILE="warfconfig.properties";
	public static String PROP_ROOT_NAME="warftool";
	public static String PROP_BASE_NAME=PROP_ROOT_NAME+".base";
	public static List<String> getAsArray(String str, String delimiter){
		List<String> resultArray=new ArrayList<String>();
		if(str != null && delimiter != null){
			String [] tempArray=str.split(delimiter);
			for(String tempStr:tempArray){
				resultArray.add(tempStr);
			}
		}
		return resultArray;
		
	}
	public static String getPropertyValue(String key){
		Properties prop = new Properties();
		try {
			prop.load(WarfUtils.class.getClassLoader().getResourceAsStream(WARF_PROPERTIES_FILE));
			String tempString=prop.getProperty(key);
			return tempString;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();

		}
		return null;
	}
	
	public static String getPropertyBaseRoot(String appName, String key){
		String levelValue=getPropertyValue(PROP_ROOT_NAME+"."+appName+"."+key);
		if(levelValue == null){
			levelValue=getPropertyValue(PROP_BASE_NAME+"."+key);
		}
		return levelValue;
	}
	
	
	public static String decryptWasPassword(String password){
		String decode = null;
        try
        {
            decode = PasswordUtil.decode(password);
        }
        catch(InvalidPasswordDecodingException e)
        {
            e.printStackTrace();
        }
        catch(UnsupportedCryptoAlgorithmException e)
        {
            e.printStackTrace();
        }
        return decode;
	}
	public static String encodeWasPassword(String password){
		WSEncoderDecoder ws= new WSEncoderDecoder();
		String encoded=ws.encode(password);	
		return encoded;
	}
	public static void main(String[] args) {
		System.out.println(decryptWasPassword("{xor}Oyg+LD47MjYxbm8="));
		System.out.println(encodeWasPassword("Pie5pie"));
		
	}
}
